package com.h.cheng.pselector.base.load;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Build;
import android.provider.MediaStore;

import com.h.cheng.pselector.bean.BaseMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： ch
 * 时间： 2018/4/10 0010-上午 9:17
 * 描述：
 * 来源：
 */


public class VideoTask extends AsyncTask<Object, Object, List<BaseMedia>> {
    private static final String CONJUNCTION_SQL = "=? or";
    private static final String SELECTION_MIME_TYPE = MediaStore.Video.Media.MIME_TYPE + CONJUNCTION_SQL + " " + MediaStore.Video.Media.MIME_TYPE + CONJUNCTION_SQL + " " + MediaStore.Video.Media.MIME_TYPE + CONJUNCTION_SQL + " " + MediaStore.Video.Media.MIME_TYPE + "=?";
    private static final String SELECTION_ID = MediaStore.Video.Media.BUCKET_ID + "=? and (" + SELECTION_MIME_TYPE + " )";


    private static final String VIDEO_MP4 = "video/mp4";
    private static final String VIDEO_AVI = "video/avi";

    private static final String[] SELECTION_ARGS_VIDEO_MIME_TYPE = {VIDEO_MP4, VIDEO_AVI};

    private static final String DESC = " desc";

    private PSConfig mPickerConfig;

    private PSManager.IMediaCallBack callBack;
    private ContentResolver resolver;
    private int page;
    private String bucketId;

    public VideoTask(PSManager.IMediaCallBack callBack, ContentResolver cr, int page, String id) {
        this.callBack = callBack;
        this.resolver = cr;
        this.page = page;
        this.bucketId = id;
    }

    @Override
    protected List<BaseMedia> doInBackground(Object[] objects) {
        List<BaseMedia> result = new ArrayList<>();
        String columns[] = getColumns();
        Cursor cursor = null;
        try {
            int totalCount = getTotalCount(resolver, bucketId, columns);

            String imageMimeType = SELECTION_MIME_TYPE;
            String[] args = SELECTION_ARGS_VIDEO_MIME_TYPE;

            String order = MediaStore.Images.Media.DATE_MODIFIED + DESC + " LIMIT "
                    + page * 1000 + " , " + 1000;
            String selectionId = SELECTION_ID;
            cursor = query(resolver, bucketId, columns, imageMimeType, args, order, selectionId);

            addItem(totalCount, result, cursor);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }


    @Override
    protected void onPostExecute(List<BaseMedia> list) {
        if (callBack != null) {
            callBack.postMedia(list, list.size());
        }
    }

    private Cursor query(ContentResolver cr, String bucketId, String[] columns,
                         String imageMimeType, String[] args, String order, String selectionId) {
        Cursor resultCursor;
        resultCursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, order);
        return resultCursor;
    }

    private void addItem(final int allCount, final List<BaseMedia> result, Cursor cursor) {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                String picPath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));

                String id = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));
                String size = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
                String mimeType = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
                int width = 0;
                int height = 0;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    width = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.WIDTH));
                    height = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.HEIGHT));
                }

                BaseMedia media = new BaseMedia(picPath, id, size);
                media.setHeight(height);
                media.setWidth(width);

                if (!result.contains(media)) {
                    result.add(media);
                }
            } while (!cursor.isLast() && cursor.moveToNext());
        } else {
        }
    }

    private String[] getColumns() {
        String[] columns;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            columns = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.WIDTH, MediaStore.Video.Media.HEIGHT};
        } else {
            columns = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.MIME_TYPE};
        }
        return columns;
    }

    private int getTotalCount(ContentResolver cr, String bucketId, String[] columns) {
        Cursor allCursor = null;
        int result = 0;
        try {
            allCursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, MediaStore.Video.Media.DATE_MODIFIED + DESC);
            if (allCursor != null) {
                result = allCursor.getCount();
            }
        } finally {
            if (allCursor != null) {
                allCursor.close();
            }
        }
        return result;
    }
}
